<?php
/**
 *  
 * @package package_name
 * @author Mervin.Wong<mervin0502@163.com>
 * @copyright
 * @license  http://www.apache.org/licenses/LICENSE-2.0
 * @version 0.1.0
 * @date 2013-2-28下午3:22:10
 *
 */
 
 /**
  * CanGoo Post Model
  * 获取文章的内容
  * @package blog
  * @subpackage models
  * @license http://www.apache.org/licenses/LICENSE-2.0 
  * @link http://mervin.me/project/cangoo description
  * 
  */
  class Post_model extends CI_Model{
  	
  	/**
  	 * 构造函数
  	 */
  	public function __construct(){
  		parent::__construct();
  		$this->load->database();
  	}
  	
  	
  	/**
  	 * 获取一组文章的ID
  	 * @access public
  	 * @param int $start 开始位置
  	 * @param int $offset 偏移量
  	 * @return array bpid数组
  	 */
  	public function get_pid($offset, $num){
  		$this->db->select('bpid');
  		$this->db->from('blog_post');
  		$this->db->order_by('addTime', 'DESC');
  		$this->db->limit($num, $offset);
  		$query = $this->db->get();
  		if($query->num_rows > 0){
  			return $query->result_array();
  		}else{
  			return false;
  		}
  		
  	}
  	
  	/**
  	 * 根据某一个tid下的文章id列表
  	 * @access public
  	 * @param int $tid description
  	 * @param int $ffset description
  	 * @param int $num description
  	 * @return array description
  	 */
  	public function get_pid_by_taxonomy($tid, $offset, $num){
  		$sql = "SELECT a.bpid FROM {$this->db->dbprefix}blog_post as a, {$this->db->dbprefix}tax_map as b WHERE a.bpid=b.pid AND b.tid={$tid}  ORDER BY a.bpid DESC LIMIT {$offset}, $num;"; 
  		$query = $this->db->query($sql);
  		return $query->result_array();
  	}
  	/**
  	 * 获取一条记录
  	 * @access public
  	 * @param array $field the field of  table
  	 * @param string $table table name
  	 * @param array $conditions the condition of sql where
  	 * @return mixed description
  	 */
  	 public function get_one($field, $table, $conditions){
  	 	$this->db->select($field);
  	 	$this->db->from($table);
  	 	foreach($conditions as $k=>$v){
  	 		$this->db->where($k, $v);
  	 	}
  	 	$query = $this->db->get();
  	 	if($query->num_rows > 0){
  	 		return $query->row();
  	 	}else{
  	 		return false;
  	 	}
  	 }
  	 
  	 /**
  	  * 获取某一个taxonomy下的文章
  	  * @access public
  	  * @param int $tid
  	  * 
  	  * @return int description
  	  */
  	 public function get_num($tid = 0, $type = ''){
		if($type == 'taxonomy'){
	  	 	$this->db->select('pid');
	  	 	$this->db->from('tax_map');
	  	 	$this->db->where('tid', $tid);
	  	 	$query = $this->db->get();
	  	 	return $query->num_rows;
		}else{
			$this->db->select('bpid');
			$this->db->from('blog_post');
			$query = $this->db->get();
			return $query->num_rows;
		}
  	 }
  	 /**
  	  * select the name and slug of the 'taxonomy' table
  	  * @access public
  	  * @param string $type taxonomy.type
  	  * @param int $pid blog_post.bpid
  	  * @return array|bool description
  	  */
  	 public function get_taxonomy_by_pid($type, $pid){
  	 	$sql = 'SELECT a.name, a.slug FROM '.$this->db->dbprefix('taxonomy').' as a, '.$this->db->dbprefix('tax_map').' as b WHERE a.tid=b.tid AND b.pid='.$pid.' AND a.type=\''.$type.'\';';
  	 	$query = $this->db->query($sql);
  	 	if($query->num_rows >0){
  	 		return $query->result_array();
  	 	}else{
  	 		return false;
  	 	}
  	 }
  	/**
  	 * 判断是否存在
  	 * @access public
  	 * @param string $table 表名
  	 * @param array $conditions 查询条件 "="
  	 * @return bool description
  	 */
  	public function is_have($table, $conditions){
  		$this->db->select();
  		$this->db->from($table);
  		foreach($conditions as $k=>$v){
  			$this->db->where($k, $v);
  		}
  		$query = $this->db->get();
  		if($query->num_rows > 0){
  			return true;
  		}else{
  			return false;
  		}
  	}
  	
  	/**
  	 * 更新对文章浏览的统计量
  	 * @access public
  	 * @param int $bpid description
  	 * @return void description
  	 */
  	public function update_view($bpid){
  		$this->db->select('viewNum');
  		$this->db->from('blog_post');
  		$this->db->where('bpid', $bpid);
  		$query = $this->db->get();
  		$row = $query->row_array();
  		
  		
  		//echo $row['viewNum']+1;
  		$this->db->set('viewNum', $row['viewNum']+1);
  		$this->db->where('bpid', $bpid);
  		$this->db->update('blog_post');
  	}
  }
?>
